package com.example.loop.controller;


import com.example.loop.entity.vo.ServerResponseVO;
import com.example.loop.enums.ServerResponseEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Liaorun
 */
@Controller
public class MyController {

    @RequestMapping({"/", "/index"})
    public String toIndex(Model model) {

        model.addAttribute("msg", "hello shiro");

        Session session = SecurityUtils.getSubject().getSession();
        Boolean login = (Boolean) session.getAttribute("login");

        if (login == null) {
            // 当前是未登录的状态
            session.setAttribute("login", false);
        }

        return "index";
    }

    @RequestMapping("/unauth")
    @ResponseBody
    public String unauth() {
        return "未经授权不能访问";
    }


    @RequiresPermissions({"add", "update", "delete", "select"})
    @RequestMapping("/user/add")
    public String add() {
        return "/user/add";
    }

    @RequiresPermissions(value = {"add", "update", "delete", "select"}, logical = Logical.OR)
    @RequestMapping("/user/update")
    public String update() {
        return "/user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "/login";
    }

    @RequestMapping("/logout")
    public String logout() {
        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        subject.getSession().setAttribute("login", false);

        // 回到首页
        return "/index";
    }


    @RequestMapping("/login")
    public String login(String username, String password, Model model) {

        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();

        // 封装用户的登录数据
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        // 执行登录方法，没有异常就是登录成功
        try {
            // 登录验证
            subject.login(usernamePasswordToken);
            ServerResponseVO.success();

            // 给前端记录信息
            Session session = SecurityUtils.getSubject().getSession();
            session.setAttribute("login", true);

            // 登录成功去首页
            return "/index";

        } catch (UnknownAccountException e) {
            ServerResponseVO.error(ServerResponseEnum.ACCOUNT_NOT_EXIST);
            model.addAttribute("msg", "ACCOUNT_NOT_EXIST");
        } catch (DisabledAccountException e) {
            ServerResponseVO.error(ServerResponseEnum.ACCOUNT_IS_DISABLED);
            model.addAttribute("msg", "ACCOUNT_IS_DISABLED");

        } catch (IncorrectCredentialsException e) {
            ServerResponseVO.error(ServerResponseEnum.INCORRECT_CREDENTIALS);
            model.addAttribute("msg", "INCORRECT_CREDENTIALS");
        } catch (Throwable e) {
            e.printStackTrace();
            ServerResponseVO.error(ServerResponseEnum.ERROR);
            model.addAttribute("msg", "ERROR");
        }

        // 登录失败继续登录
        return "/login";
    }
}
